package com.sinopec.android;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;

import android.R.color;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.sinopec.android.adapter.DocumentManagementListAdapter;
import com.sinopec.android.bean.ResultData;
import com.sinopec.android.bean.Thing;
import com.sinopec.android.bean.ThingBean;
import com.sinopec.android.bean.ThreadCallBack;
import com.sinopec.android.constant.ConfigAttrs;
import com.sinopec.android.constant.Constants;
import com.sinopec.android.net.ThreadManger;
import com.sinopec.android.util.SharePreferencesUtil;
import com.sinopec.android.util.StringUtil;
import com.sinopec.android.util.ToastUtil;

public class BusinessTripListActivity extends BaseActivity implements
		OnClickListener, ThreadCallBack {
	private TextView synctime_view;
	private TextView record_count_view,no_result_text_view;
	private ListView listView;
	
	private Button daibanBtn,yibanBtn;
	private Button prePageBtn;
	private Button nextPageBtn;
	private TextView current_page_view;
	public  ArrayList<Thing>  listData;
	DocumentManagementListAdapter documentManagementListAdapter;
	private  int pageNum = 0;
	private final int PAGESIZE = 20;
	
	private String synctime;
	private long count = 0;
	
	private int btn_flag = 3;
	
	int flagState = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.document_management_view);
		initView();
		//调接口
		sendToServer();
	}
	
	void initView(){
		((TextView)findViewById(R.id.navi)).setText("出差申请");
		synctime_view = (TextView) findViewById(R.id.synctime_textview);
		record_count_view = (TextView) findViewById(R.id.record_count_textview);
		listView = (ListView) findViewById(R.id.todo_list);
		no_result_text_view = (TextView) findViewById(R.id.no_result_text_view);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,   
                    int position, long id) {
				// TODO Auto-generated method stub
				Thing thing = listData.get(position);
//				if("收文审批".equals(thing.getType())){
//					Intent intent = new Intent();
//					intent.setClass(getApplicationContext(), DocumentDetailActivity.class);
//					thing.setYiBan(flagState==1);
//					intent.putExtra("thing", thing);
//					startActivity(intent);
//					finish();
//				}
				if(thing.getType()!=null&&thing.getType().contains("出差")){
					if(thing.getType().contains("正")){
						thing.setLevelNum(0);
					}
					if(thing.getType().contains("副")){
						thing.setLevelNum(1);
					}
					if(thing.getType().contains("员工")){
						thing.setLevelNum(2);
					}
					thing.setYiBan(flagState==1);
					Intent intent = new Intent();
					intent.setClass(getApplicationContext(), BusinessTripActivity.class);
					intent.putExtra("thing", thing);
					startActivity(intent);
					finish();
				}
			}
		});
		
		daibanBtn = (Button) findViewById(R.id.daiban_btn);
		daibanBtn.setOnClickListener(this);
		
		yibanBtn = (Button) findViewById(R.id.yiban_btn);
		yibanBtn.setOnClickListener(this);
		
		
		prePageBtn = (Button) findViewById(R.id.pre_page_btn);
		prePageBtn.setOnClickListener(this);

		
		current_page_view = (TextView) findViewById(R.id.current_page_textview);
		
		nextPageBtn = (Button) findViewById(R.id.next_page_btn);
		nextPageBtn.setOnClickListener(this);
		
	}
	@Override
	public void onCallbackFromThread(ResultData resultData) {
		// TODO Auto-generated method stub
		if(resultData!=null && resultData.getResultList()!=null){
		}else{
			if(btn_flag==3){//待办
				
			}else if(btn_flag==4){//已办
				
			}else if(btn_flag==1){
				pageNum++;
			}else if(btn_flag==2){
				pageNum--;
			}
			Toast.makeText(this,R.string.get_fail, 0).show();
			return;
		}
		if(resultData.getResultList().get(0) instanceof String){
			if(btn_flag==3){//待办
				
			}else if(btn_flag==4){//已办
				
			}else if(btn_flag==1){
				pageNum++;
			}else if(btn_flag==2){
				pageNum--;
			}
			Toast.makeText(this,resultData.getResultList().get(0).toString(), 0).show();
			return ;
		}
		ArrayList arrayList = resultData.getResultList();
		//查询通讯录结果
		if(arrayList.size()>0&&arrayList.get(0) instanceof ThingBean){
			ThingBean thingBean = (ThingBean) arrayList.get(0);
			
			if(thingBean!=null){
				count = thingBean.getCount();
				if(count!=0){
				record_count_view.setText("共"+count+"条记录");
				}
				if(btn_flag==3){//待办
					if(flagState!=0){
						flagState = 0;
					}
					Constants.travelToDo = count;
					daibanBtn.setBackgroundResource(R.drawable.btn_short_lightblue);
					yibanBtn.setBackgroundResource(R.drawable.btn_short_lightgray);
				}else if(btn_flag==4){//已办
					if(flagState!=1){
						flagState = 1;
					}
					daibanBtn.setBackgroundResource(R.drawable.btn_short_lightgray);
					yibanBtn.setBackgroundResource(R.drawable.btn_short_lightblue);
				}
				try{
					SimpleDateFormat   sDateFormat   =   new   SimpleDateFormat("yyyy-MM-dd hh:mm:ss");      
					synctime  =   sDateFormat.format(new   java.util.Date());   
					synctime_view.setText(getResources().getString(R.string.sync_time_text)+synctime);
				}catch (Exception e) {
					// TODO: handle exception
				}
				
				listData = thingBean.getList();
				current_page_view.setText("第"+(pageNum+1)+"页");
				if(listData==null||(listData!=null&&listData.isEmpty())){
					listView.setVisibility(View.GONE);
					no_result_text_view.setVisibility(View.VISIBLE);
				}else{
					listView.setVisibility(View.VISIBLE);
					no_result_text_view.setVisibility(View.GONE);
					if(documentManagementListAdapter!=null){
						documentManagementListAdapter.listData = listData;
						documentManagementListAdapter.notifyDataSetChanged();
					}else{
						documentManagementListAdapter = new DocumentManagementListAdapter(this,listData);
						listView.setAdapter(documentManagementListAdapter);
					}
				}
				
			}
			
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.daiban_btn://待办按钮
			btn_flag = 3;
			if(flagState!=0){
				flagState = 0;
			}
			sendToServer();
			break;
		case R.id.yiban_btn://已办按钮
			btn_flag = 4;
			if(flagState!=1){
				flagState = 1;
			}
			sendToServer();
			break;
		case R.id.pre_page_btn://上一页
			if("第1页".equals(current_page_view.getText())){
				ToastUtil.showToast(getApplicationContext(), "抱歉，没有上一页啦");
				return ;
			}
			btn_flag = 1;
			pageNum--;
			sendToServer();
			break;
		case R.id.next_page_btn://下一页
			long totalPage = count/PAGESIZE;
			if(count%PAGESIZE>0){
				totalPage = count/PAGESIZE+1;
			}
			if(("第"+totalPage+"页").equals(current_page_view.getText())){
				ToastUtil.showToast(getApplicationContext(), "抱歉，已经是最后一页啦");
				return;
			}
			btn_flag = 2;
			pageNum++;
			sendToServer();
			break;
		default:
			break;
		}
	}
	
	void  sendToServer(){
		String reqUrl =ConfigAttrs.BASE_URL+"GetToDoThingsListByKqgl.ashx";
		HashMap<String, String> hashMap = new HashMap<String, String>();
		hashMap.put("userName", SharePreferencesUtil.getLastLoginAccount(getApplicationContext(), ConfigAttrs.LAST_LOGIN_ACCOUNT));
		hashMap.put("passWord", SharePreferencesUtil.getLastLoginAccount(getApplicationContext(), ConfigAttrs.LAST_LOGIN_PWD));
		if(StringUtil.isEmpty(deviceId)){
			deviceId = "0000000000";
		}
		hashMap.put("deviceId", deviceId);
		hashMap.put("pageNum", pageNum+"");
		hashMap.put("pageAccount", PAGESIZE+"");
		hashMap.put("flagState", flagState+"");

		ThreadManger.exeTask(this, ConfigAttrs.TODO_THING_TASK_FLAG, hashMap, reqUrl);
	}
}
